package com.morontestandroid;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class SixthLevel extends Activity implements
android.view.View.OnClickListener{
    /** Called when the activity is first created. */
	
	AnimationDrawable AniFrame;
	ImageView MyImageView;
	ImageView bush1,bush2,bush3;
	ImageView backG;
	Chronometer mChronometer;
    
	boolean bush1clicked, bush2clicked, bush3clicked, bush4clicked;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sixth_level);
        

          MyImageView = (ImageView)findViewById(R.id.myImageView);
          bush1 = (ImageView)findViewById(R.id.imageView5);
          bush2 = (ImageView)findViewById(R.id.imageView2);
          bush3 = (ImageView)findViewById(R.id.imageView6);
          mChronometer = (Chronometer) findViewById(R.id.level6ChrmId);
          backG =  (ImageView)findViewById(R.id.background);
          
          mChronometer.setBase(getTime()); 
  		  mChronometer.start();
          
          bush1clicked = false;
          bush2clicked = false;
          bush3clicked = false;
          bush4clicked = false;
               

          
          MyImageView.setBackgroundResource(R.drawable.run_animation);
       
        AniFrame = (AnimationDrawable) MyImageView.getBackground();
        
       // MyImageView.bringToFront();
        backG.setOnClickListener(this);
        MyImageView.setOnClickListener(this);
        bush1.setOnClickListener(this);
        bush2.setOnClickListener(this);
        bush3.setOnClickListener(this);
        
        
	       TextView txt = (TextView) findViewById(R.id.custom_font);  
	       Typeface font = Typeface.createFromAsset(getAssets(), "testtext.ttf");  
	        txt.setTypeface(font);
    }
    
    public long getTime() {
		  Bundle extras = getIntent().getExtras();
		  long time = extras.getLong("time");
		  return time;
		 
	}
    
    public void startActivity() {
    	int activityID = 1;
		Intent intent;
		intent = new Intent().setClass(this, SeventhLevel.class);
		intent.putExtra("time", getThisBase());
		startActivityForResult(intent, activityID);
    }
    
    public void onWindowFocusChanged(boolean hasFocus) {

        super.onWindowFocusChanged(hasFocus);
        
		 Animation firstAni = new TranslateAnimation(0, 100, 0, 245);
	        firstAni.setDuration(2000);

      if(hasFocus) {
	        firstAni.setAnimationListener(new AnimationListener(){

	            public void onAnimationStart(Animation animation) {
	            // do nothing       
	            }
	            public void onAnimationRepeat(Animation animation) {
	            // do nothing       
	            }
                  // at the end of the animation
	            public void onAnimationEnd(Animation animation) {
	            	setLocation(100,245);
	            	bush1clicked=true;
	            }
	});
		        MyImageView.startAnimation(firstAni);
		       // AniFrame.run();      // needed to repeat the animation if OneShot is set to true
				AniFrame.start();
      }
}

	public void onClick(View view) {
		if(view.getId()==R.id.imageView5 && bush1clicked==true){
			 Animation secondAni = new TranslateAnimation(0, 245, 0, -100);
		        secondAni.setDuration(2000);
		        
		        secondAni.setAnimationListener(new AnimationListener(){

		            public void onAnimationStart(Animation animation) {
		            // do nothing       
		            }
		            public void onAnimationRepeat(Animation animation) {
		            // do nothing       
		            }
	                  // at the end of the animation
		            public void onAnimationEnd(Animation animation) {
		            	 setLocation(345,145);
		            	bush2clicked=true;
		            }
		});
		        
		        MyImageView.startAnimation(secondAni);
		        bush1.setVisibility(View.GONE);
		       
}
		if(view.getId()==R.id.imageView2 && bush2clicked==true){	
			 Animation thirdAni = new TranslateAnimation(0, 165, 0, 180);
			 thirdAni.setDuration(2000);
			 thirdAni.setAnimationListener(new AnimationListener(){

		            public void onAnimationStart(Animation animation) {
		            // do nothing       
		            }
		            public void onAnimationRepeat(Animation animation) {
		            // do nothing       
		            }
	                  // at the end of the animation
		            public void onAnimationEnd(Animation animation) {
		            	 setLocation(510,325);
		            	bush3clicked=true;
		            }
		});
		        
		        MyImageView.startAnimation(thirdAni);
		        bush2.setVisibility(View.GONE);
		}
		if(view.getId()==R.id.imageView6 && bush3clicked==true){	
			
			Animation fourthAni = new TranslateAnimation(0, 360, 0, -100);
			 fourthAni.setDuration(2000);
			 
			 fourthAni.setAnimationListener(new AnimationListener(){

		            public void onAnimationStart(Animation animation) {
		            // do nothing       
		            }
		            public void onAnimationRepeat(Animation animation) {
		            // do nothing       
		            }
	                  // at the end of the animation
		            public void onAnimationEnd(Animation animation) {
		          
		        // endOfLevel();
		            	
		                startActivity();
		            }
		});
		        
		        MyImageView.startAnimation(fourthAni);
		        bush3.setVisibility(View.GONE);
		    	
		    
		}
	

if(view.getId()==R.id.background  || view.getId()==R.id.imageView2 && bush2clicked==false || view.getId()==R.id.imageView5 && bush1clicked==false || view.getId()==R.id.imageView6 && bush3clicked==false){
	startActivity(new Intent(this, CheckPoint.class));
}
}
	private void endOfLevel(){
		 startActivity(new Intent(this, CheckPoint.class));
	}
	private void setLocation(int x, int y) {
	    final ImageView iv = (ImageView) findViewById(R.id.myImageView);
	    final AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) iv.getLayoutParams();
	    lp.x = x;
	    lp.y = y;
	    iv.setLayoutParams(lp);
	 
	}
	public void onBackPressed() {
		// do something on back.
		startActivity(new Intent(this, MenyActivity.class));
		return;
		}
	
	public long getThisBase(){
		  return mChronometer.getBase();
		 
	}
	

}